<?php


namespace app\zc\controller;


use app\zc\model\NewsModel;
use app\zc\validata\NewsValidata;
use cmf\controller\AdminBaseController;
use think\Request;


class NewsController extends AdminBaseController
{
    public function index()
    {
        //搜索
        $keyword = input('keyword/s','');
        $where = [];
        if($keyword){
            $where[] = ['title|author','like','%'.$keyword.'%'];
        }

        //新闻公告列表显示
        $list = NewsModel::order(['list_order'=>'DESC','id'=>'DESC'])
            ->where($where)
            ->paginate(5);

        $this->assign('list',$list);
        return $this->fetch('news');

    }

    public function px(){
        $news = new NewsModel();
        parent::listOrders($news);
        $this->success('排序成功');
    }

    public function content(){
        $id = input('id','');
        $list = NewsModel::find($id);
        //富文本处理
        $content = htmlspecialchars_decode($list['content']);
        $content1 = cmf_replace_content_file_url($content);
        $list['content'] = $content1;

        $this->assign('list',$list);
        return $this->fetch('content');
    }

    public function show(Request $request){
        $id = $request->param('id');

        $list = NewsModel::find($id);

        if($list['switch'] == 1){
            $list['switch'] = 0;
        }else{
            $list['switch'] = 1;
        }

        $edit = NewsModel::find($id);
        $edit->switch = $list['switch'];
        if($edit->save()){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }

    }


    //添加页面显示
    public function add()
    {
        return $this->fetch();
    }

    //添加逻辑
    public function addPost(Request $request)
    {

        //获取字段
        $data = input('post.');
        $title = $request->param('title');
        $author = $request->param('author');
        $look_num = $request->param('look_num');
        $abstract = $request->param('abstract');
        $switch = $request->param('switch');
        $create_time = date('Y-m-d H-i-s');

        //富文本处理
        $content = htmlspecialchars_decode($data['content']);
        $content = cmf_replace_content_file_url($content,true);
        $content = htmlspecialchars($content);

        //图片获取器
        $image = $_POST['image'];
        $image = cmf_get_image_url($image);

        //验证器验证
        $validata = new NewsValidata();
        $result = $validata->scene('add')->check($data);
        if(!$result){
            $this->error($validata->getError());
        }

        $add = NewsModel::create([
            'title'=>$title,
            'image'=>$image,
            'abstract'=>$abstract,
            'content'=>$content,
            'look_num'=>$look_num,
            'author'=>$author,
            'switch'=>$switch,
            'create_time'=>$create_time
        ]);

        if($add){
            $this->success('添加成功','News/index');
        } else {
            $this->error('添加失败');
        }
    }
    //编辑页面显示
    public function edit(){
        $id = input('id','');

        $list = NewsModel::find($id);

        //富文本处理
        $content = htmlspecialchars_decode($list['content']);
        $content1 = cmf_replace_content_file_url($content);
        $list['content'] = $content1;

        $this->assign('list',$list);
        return $this->fetch();

    }

    //编辑
    public function editPost(){

        $data = input('post.');

        //验证器验证
        $validata = new NewsValidata();
        $result = $validata->scene('edit')->check($data);
        if(!$result){
            $this->error($validata->getError());
        }

        //图片获取器
        $data['image'] = cmf_get_image_url($data['image']);

        $edit = NewsModel::find($data['id']);
        if(!$edit){
            $this->error('未查询到数据');
        }
        $edit->title = $data['title'];
        $edit->image = $data['image'];
        $edit->abstract = $data['abstract'];
        $edit->contnent = $data['content'];
        $edit->look_num = $data['look_num'];
        $edit->author = $data['author'];
        $edit->switch = $data['switch'];

        if($edit->save()){
            $this->success('保存成功','News/index');
        }else{
            $this->error('保存失败');
        }
    }
    //删除
    public function delete(){
        $id = input('id','');
        if(NewsModel::destroy($id)){
            $this->success('删除成功','news/index');
        }else{
            $this->error('删除失败');
        }
    }

}